package org.nch.videoLauncher.view;

import org.nch.videoLauncher.presenter.VideoDetailsPresenter;
import org.nch.videoLauncher.domain.PlaybackEvent;

import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import java.text.SimpleDateFormat;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Jan 3, 2009
 * Time: 4:21:05 PM
 * To change this template use File | Settings | File Templates.
 */
public class VideoDetailsView implements IVideoDetailsView, IContentPanel {
    private JPanel videoDetailsPanel;
    private JTabbedPane videoDetailsTabPane;
    private JPanel movieTab;
    private JPanel tvTab;
    private JPanel sportsTab;
    private JPanel historyTab;
    private JPanel titlePanel;
    private JLabel titleLabel;
    private JPanel moviePanel;
    private JPanel coverPanel;
    private JTextArea descriptionText;
    private JLabel movieCoverLabel;
    private JScrollPane descriptionScrollPane;
    private JPanel bottomPanel;
    private JTextField directorContent;
    private JTextField mpaaContent;
    private JTextField runtimeContent;
    private JLabel keywordsContent;
    private JLabel directedByContent;
    private JTextField ratingContent;
    private JTextField yearContent;
    private JLabel yearLabel;
    private JLabel runtimeLabel;
    private JLabel ratingLabel;
    private JLabel mpaaLabel;
    private JLabel directorLabel;
    private JTextField genreContent;
    private JLabel genreLabel;
    private JTextField titleText;
    private JPanel buttonPanel;
    private JButton webButton;
    private JButton favoriteButton;
    private JButton moviePlayButton;
    private JPanel tvPanel;
    private JPanel tvtitlePanel;
    private JPanel tvcoverPanel;
    private JPanel tvdetailsPanel;
    private JPanel tvbuttonsPanel;
    private JTextField tvTitleText;
    private JTextArea tvDescriptionText;
    private JLabel tvCoverLabel;
    private JScrollPane tvDescriptionScrollPane;
    private JLabel tvShowLabel;
    private JLabel tvSeasonLabel;
    private JLabel tvEpisodeLabel;
    private JLabel tvRatingLabel;
    private JLabel tvGenreLabel;
    private JButton tvFavouriteButton;
    private JButton tvWebButton;
    private JButton tvPlayButton;
    private JPanel sportsPanel;
    private JPanel sportsTitlePanel;
    private JTextField sportsTitleText;
    private JPanel sportsDescriptionPanel;
    private JScrollPane sportsDescriptionScrollPane;
    private JTextArea sportsDescriptionText;
    private JLabel sportSportLabel;
    private JLabel sportTeamLabel;
    private JLabel sportSeasonLabel;
    private JTextField sportSportText;
    private JTextField sportTeamText;
    private JTextField sportSeasonText;
    private JButton sportFavouriteButton;
    private JButton sportsWebButton;
    private JButton sportsPlayButton;
    private JTextField sportsKeywordsText;
    private JLabel sportsKeywordsLabel;
    private JTextField tvGenreText;
    private JTextField tvShowText;
    private JTextField tvSeasonText;
    private JTextField tvEpisodeText;
    private JTextField tvRatingText;
    private JPanel historyPanel;
    private JScrollPane historyScrollPane;
    private JTable historyTable;

    private VideoDetailsPresenter presenter;
    private List<PlaybackEvent> playbackEvents;
    private tableModel historyTableModel;

    public VideoDetailsView() {
        moviePlayButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.playRequested();
            }
        });
        tvPlayButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.playRequested();
            }
        });
        sportsPlayButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.playRequested();
            }
        });
        webButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.openUrlRequested();
            }
        });
        tvWebButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.openUrlRequested();
            }
        });
        sportsWebButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.openUrlRequested();
            }
        });
        videoDetailsTabPane.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent event) {
                JTabbedPane tp = (JTabbedPane) event.getSource();

                if (tp.getSelectedIndex() == 3){
                    presenter.showHistory();    
                }
            }
        });
    }

    public JPanel getRootComponent() {
        return videoDetailsPanel;
    }

    public void setPresenter(VideoDetailsPresenter presenter){
        this.presenter = presenter;
    }    

    public void setMovieTitleText(String title) {
        titleText.setText(title);
    }

    public void setMovieGenreText(String keywords) {
        genreContent.setText(keywords);
    }

    public void setMovieDescriptionText(String description) {
        descriptionText.setText(description);
    }

    public void setMovieDirectedByText(String directedby) {
        directorContent.setText(directedby);
    }

    public void setMovieRatingText(String rating) {
        ratingContent.setText(rating);
    }

    public void setMovieYearText(String date) {
        yearContent.setText(date);
    }

    public void setMovieMpaaText(String mpaa){
        mpaaContent.setText(mpaa);
    }

    public void setMovieRuntime(String runtime){
        runtimeContent.setText(runtime);
    }

    public void setMovieCover(byte[] cover) {

        if (cover != null){
            Image coverImage = Toolkit.getDefaultToolkit().createImage(cover);
            ImageIcon coverImageIcon = new ImageIcon(coverImage);
            movieCoverLabel.setIcon(coverImageIcon);            
        }
    }

    public void setMovieOpenUrlEnabled(boolean enabled) {
        webButton.setEnabled(enabled);
    }

    public void setTvTitleText(String title) {
        tvTitleText.setText(title);
    }

    public void setTvGenreText(String keywords) {
        tvGenreText.setText(keywords);
    }

    public void setTvDescriptionText(String description) {
        tvDescriptionText.setText(description);
    }

    public void setTvShow(String tvshow) {
        tvShowText.setText(tvshow);
    }

    public void setTvSeason(String tvseason) {
        tvSeasonText.setText(tvseason);
    }

    public void setTvEpisode(String episode) {
        tvEpisodeText.setText(episode);
    }

    public void setTvRatingText(String rating) {
        tvRatingText.setText(rating);
    }

    public void setTvCover(byte[] cover) {
        if (cover != null){
            Image coverImage = Toolkit.getDefaultToolkit().createImage(cover);
            ImageIcon coverImageIcon = new ImageIcon(coverImage);
            tvCoverLabel.setIcon(coverImageIcon);
        }
    }

    public void setTvOpenUrlEnabled(boolean enabled) {
        tvWebButton.setEnabled(enabled);
    }

    public void setSportsTitleText(String title) {
        sportsTitleText.setText(title);
    }

    public void setSportsDescriptionText(String description) {
        sportsDescriptionText.setText(description);
    }

    public void setSportsSport(String sport) {
        sportSportText.setText(sport);
    }

    public void setSportsTeam(String team) {
        sportTeamText.setText(team);
    }

    public void setSportsSeason(String season) {
        sportSeasonText.setText(season);
    }

    public void setSportsKeywords(String keywords) {
        sportsKeywordsText.setText(keywords);
    }

    public void setSportsOpenUrlEnabled(boolean enabled) {
        sportsWebButton.setEnabled(enabled);
    }

    public void setMovieTabEnabled(boolean enabled) {
        videoDetailsTabPane.setEnabledAt(0, enabled);
    }

    public void setTvTabEnabled(boolean enabled) {
        videoDetailsTabPane.setEnabledAt(1, enabled);
    }

    public void setSportsTabEnabled(boolean enabled) {
        videoDetailsTabPane.setEnabledAt(2, enabled);
    }

    public void showMovieTab() {
        videoDetailsTabPane.setSelectedIndex(0);
    }

    public void showTvTab() {
        videoDetailsTabPane.setSelectedIndex(1);
    }

    public void showSportsTab() {
        videoDetailsTabPane.setSelectedIndex(2);
    }

    public void setHistoryList(List<PlaybackEvent> playbackEvents) {
        this.playbackEvents = playbackEvents;
        historyTableModel = new tableModel();
        historyTable.setModel(historyTableModel);
        historyTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    }

    private class tableModel extends AbstractTableModel {

            public int getRowCount(){ return playbackEvents.size();}
            public int getColumnCount(){ return 2;}

            public String getColumnName(int col){    // TODO:  need a cleaner implementation of the table column names
                String headings[] = new String[]{"Playback Date & Time", "Playback Profile"};
                return headings[col];
            }

            public Object getValueAt(int row, int col) {
                if (col == 0) {
                    SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM d yyyy, H:mm");

                    return formatter.format(playbackEvents.get(row).getDate());                    
                }
                if (col == 1)
                    return playbackEvents.get(row).getProfile().getName();
                else
                    return "";
            }
    };


}
